const db = require('../../lib/conn_oracle')

//获取站点信息
exports.getCrtStations = async (req,res)=>{
    let resDatas
    let status
    let msg
    let sql = `select location,description
               from c##wxw.location
               where cust_linenum = 'L3B'
                 and areatype <> '线路'
               order by location`
    try{
        let stations = await db.search2(sql)
        status =  stations.errorNum ? 0 : 1
        msg = stations.errorNum? "查询失败" : "查询成功"
        resDatas = {stations}
        res.json({
            status,
            msg,
            resDatas
        })
    }catch(err){
        res.json({
            status:0,
            msg:"查询失败",
            resDatas:{err}
        })
    }
}

//获取CRT信息
exports.getCrtListInfo = async (req,res)=>{
    // console.log(req.body);
    let {begindate,enddate,stations,sbdesc} = req.body
    let deviceDatas = []
    deviceDatas.push(begindate)
    deviceDatas.push(enddate)
    //线路条件
    let stationsWhere
    if(stations && stations.length > 1){
        stationsWhere = `and c.location in (`
        for (let i = 0; i < stations.length; i++)
        stationsWhere += (i > 0) ? ", :" + i : ":" + i
        stationsWhere += ")"
        deviceDatas.push(...stations)
    }else if(stations && stations.length === 1){
        stationsWhere = `and c.location = :stations` 
        deviceDatas.push(...stations)
    }else{stationsWhere = ""}
    //描述条件
    let descLike  
    let descWhere 
    if(sbdesc.length > 0){
        descLike = `%${sbdesc}%`
        descWhere = `and c.devdesc like :descLike`
        deviceDatas.push(descLike)
    }else{
        descLike = ""
        descWhere = ""
    }
    let resDatas
    let status
    let msg
    let sql = `select c.*,l.description as locDesc
               from c##wxw.crt_list c
               inner join c##wxw.location l 
               on c.location = l.location
               where c.eventtime >= to_timestamp(:begindate,'yyyy-mm-dd hh24:mi:ss')
                 and c.eventtime <= to_timestamp(:enddate,'yyyy-mm-dd hh24:mi:ss')
                 ${stationsWhere}
                 ${descWhere}
               order by c.location,c.eventtime
               `
    try{
        let crtList = await db.search(sql,deviceDatas)
        status =  crtList.errorNum ? 0 : 1
        msg = crtList.errorNum? "查询失败" : "查询成功"
        resDatas = {crtList}
        res.json({
            status,
            msg,
            resDatas
        })
    }catch(err){
        res.json({
            status:0,
            msg:"查询失败",
            resDatas:{err}
        })
    }
    // res.send("ok")
}

//编辑CRT信息
exports.editCrtListInfo = async (req,res) => {
    console.log(req.body)
    let { type,form } = req.body
    // let { ID,DESCRIPTION,AREA,AREA2,STATUS,CUST_LINENUM,LOCATION,
    //     CUST_SPECNUM,CUST_SUBSYS,CUST_USETEAM,ISJIANCE,JCDATE,JCLAST,
    //     DEVICENUM,SBDESC,REMARK
    // } = form 
    let { ID } = form
    let resDatas
    let msg
    let msgType
    let resStatus
    let editSql 
    let editDatas = []
    //#region 
    /*if(type === 'add' || type === 'copy'){
        msgType = "新增"
        editDatas = [DESCRIPTION,AREA,AREA2,STATUS,CUST_LINENUM,LOCATION,
            CUST_SPECNUM,CUST_SUBSYS,CUST_USETEAM,ISJIANCE,JCDATE,JCLAST,
            DEVICENUM,SBDESC,REMARK]
        editSql = `insert into c##wxw.device(id,description,area,area2,status,cust_linenum,location,cust_specnum,cust_subsys,cust_useteam,isjiance,jcdate,jclast,devicenum,sbdesc,remark)
                    values('',:DESCRIPTION,:AREA,:AREA2,:STATUS,:CUST_LINENUM,:LOCATION,:CUST_SPECNUM,:CUST_SUBSYS,:CUST_USETEAM,:ISJIANCE
                    ,to_timestamp(:JCDATE,'yyyy-mm-dd hh24:mi:ss.ff'),to_timestamp(:JCLAST,'yyyy-mm-dd hh24:mi:ss.ff'),:DEVICENUM,:SBDESC,:REMARK)
                    ` 
    }
    if(type === 'update'){
        msgType = "更新"
        editDatas = [DESCRIPTION,AREA,AREA2,STATUS,CUST_LINENUM,LOCATION,
            CUST_SPECNUM,CUST_SUBSYS,CUST_USETEAM,ISJIANCE,JCDATE,JCLAST,
            DEVICENUM,SBDESC,REMARK,ID]
        editSql = `update c##wxw.device
                    set description = :DESCRIPTION,
                    area = :AREA,
                    area2 = :AREA2,
                    status = :STATUS,
                    cust_linenum = :CUST_LINENUM,
                    location = :LOCATION,
                    cust_specnum = :CUST_SPECNUM,
                    cust_subsys = :CUST_SUBSYS,
                    cust_useteam = :CUST_USETEAM,
                    isjiance = :ISJIANCE,
                    jcdate = to_timestamp(:JCDATE,'yyyy-mm-dd hh24:mi:ss.ff'),
                    jclast = to_timestamp(:JCLAST,'yyyy-mm-dd hh24:mi:ss.ff'),
                    devicenum = :DEVICENUM,
                    sbdesc = :SBDESC,
                    remark = :REMARK                    
                    where id = :ID`
    }*/
   //#endregion
    if(type === 'delete'){
        msgType = "删除"
        editDatas= [ID]
        editSql = `delete from c##wxw.crt_list where id = :ID` 
    }
    try{
        let newCrtList = await db.writeTab(editSql,editDatas)
        // console.log(newDevice)
        resDatas = {newCrtList}
        msg = !resDatas.newCrtList.errorNum ? `${msgType}成功` : `${msgType}失败`
        resStatus = !resDatas.newCrtList.errorNum ? 1 : 0
        res.json({
            status:resStatus,
            msg,
            resDatas
        })
    }catch(err){
        // console.log(err)
        res.json({
            status:0,
            msg:`${msgType}失败`,
            resDatas:{err}
        })
    }
}

//批量编辑CRT信息
exports.editManyCrtListInfo = async (req,res) => {
    // console.log(req.body)
    let { type,form } = req.body  
    console.log(type)
    console.log(form)
    let resDatas
    let msg
    let msgType
    let resStatus
    let editSql
    let options = {
        autoCommit:true,
    }
    if(type === 'batchDel'){
        msgType = "批量删除"
        editSql = `delete from c##wxw.crt_list where id = :ID`
    }
    if(type === 'batchAdd'){
        msgType = "批量新增"
        editSql = `insert into c##wxw.crt_list(id,description,devicenum,status,devdesc,eventtime,location)
                                    values(:ID,:DESCRIPTION,:DEVICENUM,:STATUS,:DEVDESC,to_timestamp(:EVENTTIME,'yyyy-mm-dd hh24:mi:ss.ff'),:LOCATION)
                                            `
    }
    if(type === 'batchUpdate'){
        msgType = "批量更新"
        editSql = `update c##wxw.crt_list
                    set description = :DESCRIPTION,
                    devicenum = :DEVICENUM,
                    status = :STATUS,
                    devdesc = :DEVDESC,
                    eventtime = to_timestamp(:EVENTTIME,'yyyy-mm-dd hh24:mi:ss.ff'),
                    location = :LOCATION,
                    where id = :ID`
    }
    try{
        let newCrtList = await db.writeManyTab(editSql,form,options)
        // console.log(newDevice)
        resDatas = {newCrtList}
        msg =!resDatas.newCrtList.errorNum ? `${msgType}成功` : `${msgType}失败`
        resStatus =!resDatas.newCrtList.errorNum? 1 : 0
        res.json({
            status:resStatus,
            msg,
            resDatas
        })
    }catch(err){
        // console.log(err) 
        res.json({
            status:0,
            msg:`${msgType}失败`,
            resDatas:{err}  
        }) 
    }
}